<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/extension.qdoc -->
<head>
  <title>Qt 4.3: Extension Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Extension Example<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="dialogs-extension-finddialog-cpp.html">dialogs/extension/finddialog.cpp</a></li>
<li><a href="dialogs-extension-finddialog-h.html">dialogs/extension/finddialog.h</a></li>
<li><a href="dialogs-extension-main-cpp.html">dialogs/extension/main.cpp</a></li>
</ul>
<p>The Extension example shows how to add an extension to a <a href="qdialog.html">QDialog</a> using the <a href="qabstractbutton.html#toggled">QAbstractButton::toggled</a>() signal and the <a href="qwidget.html#visible-prop">QWidget::setVisible</a>() slot.</p>
<p align="center"><img src="images/extension-example.png" alt="Screenshot of the Extension example" /></p><p>The Extension application is a dialog that allows the user to perform a simple search as well as a more advanced search.</p>
<p>The simple search has two options: <b>Match case</b> and <b>Search from start</b>. The advanced search options include the possibilities to search for <b>Whole words</b>, <b>Search backward</b> and <b>Search selection</b>. Only the simple search is visible when the application starts. The advanced search options are located in the application's extension part, and can be made visible by pressing the <b>More</b> button:</p>
<p align="center"><img src="images/extension_more.png" alt="Screenshot of the Extension example" /></p><a name="finddialog-class-definition"></a>
<h2>FindDialog Class Definition</h2>
<p>The <tt>FindDialog</tt> class inherits <a href="qdialog.html">QDialog</a>. The <a href="qdialog.html">QDialog</a> class is the base class of dialog windows. A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user.</p>
<pre> class FindDialog : public QDialog
 {
     Q_OBJECT

 public:
     FindDialog(QWidget *parent = 0);

 private:
     QLabel *label;
     QLineEdit *lineEdit;
     QCheckBox *caseCheckBox;
     QCheckBox *fromStartCheckBox;
     QCheckBox *wholeWordsCheckBox;
     QCheckBox *searchSelectionCheckBox;
     QCheckBox *backwardCheckBox;
     QDialogButtonBox *buttonBox;
     QPushButton *findButton;
     QPushButton *moreButton;
     QWidget *extension;
 };</pre>
<p>The <tt>FindDialog</tt> widget is the main application widget, and displays the application's search options and controlling buttons.</p>
<p>In addition to a constructor, we declare the several child widgets: We need a <a href="qlineedit.html">QLineEdit</a> with an associated <a href="qlabel.html">QLabel</a> to let the user type a word to search for, we need several <a href="qcheckbox.html">QCheckBox</a>es to facilitate the search options, and we need three <a href="qpushbutton.html">QPushButton</a>s: the <b>Find</b> button to start a search, the <b>More</b> button to enable an advanced search, and the <b>Close</b> button to exit the application. Finally, we need a <a href="qwidget.html">QWidget</a> representing the application's extension part.</p>
<a name="finddialog-class-implementation"></a>
<h2>FindDialog Class Implementation</h2>
<p>In the constructor we first create the standard child widgets for the simple search: the <a href="qlineedit.html">QLineEdit</a> with the associated <a href="qlabel.html">QLabel</a>, two of the <a href="qcheckbox.html">QCheckBox</a>es and all the <a href="qpushbutton.html">QPushButton</a>s.</p>
<pre> FindDialog::FindDialog(QWidget *parent)
     : QDialog(parent)
 {
     label = new QLabel(tr(&quot;Find &amp;what:&quot;));
     lineEdit = new QLineEdit;
     label-&gt;setBuddy(lineEdit);

     caseCheckBox = new QCheckBox(tr(&quot;Match &amp;case&quot;));
     fromStartCheckBox = new QCheckBox(tr(&quot;Search from &amp;start&quot;));
     fromStartCheckBox-&gt;setChecked(true);

     findButton = new QPushButton(tr(&quot;&amp;Find&quot;));
     findButton-&gt;setDefault(true);

     moreButton = new QPushButton(tr(&quot;&amp;More&quot;));
     moreButton-&gt;setCheckable(true);</pre>
<p>We give the options and buttons a shortcut key using the &amp; character. In the <b>Find what</b> option's case, we also need to use the <a href="qlabel.html#setBuddy">QLabel::setBuddy</a>() function to make the shortcut key work as expected; then, when the user presses the shortcut key indicated by the label, the keyboard focus is transferred to the label's buddy widget, the <a href="qlineedit.html">QLineEdit</a>.</p>
<p>We set the <b>Find</b> button's default property to true, using the <a href="qpushbutton.html#default-prop">QPushButton::setDefault</a>() function. Then the push button will be pressed if the user presses the Enter (or Return) key. Note that a <a href="qdialog.html">QDialog</a> can only have one default button.</p>
<pre>     extension = new QWidget;

     wholeWordsCheckBox = new QCheckBox(tr(&quot;&amp;Whole words&quot;));
     backwardCheckBox = new QCheckBox(tr(&quot;Search &amp;backward&quot;));
     searchSelectionCheckBox = new QCheckBox(tr(&quot;Search se&amp;lection&quot;));</pre>
<p>Then we create the extension widget, and the <a href="qcheckbox.html">QCheckBox</a>es associated with the advanced search options.</p>
<pre>     connect(moreButton, SIGNAL(toggled(bool)), extension, SLOT(setVisible(bool)));

     QVBoxLayout *extensionLayout = new QVBoxLayout;
     extensionLayout-&gt;setMargin(0);
     extensionLayout-&gt;addWidget(wholeWordsCheckBox);
     extensionLayout-&gt;addWidget(backwardCheckBox);
     extensionLayout-&gt;addWidget(searchSelectionCheckBox);
     extension-&gt;setLayout(extensionLayout);</pre>
<p>Now that the extension widget is created, we can connect the <b>More</b> button's <a href="qabstractbutton.html#toggled">toggled()</a> signal to the extension widget's <a href="qwidget.html#visible-prop">setVisible()</a> slot.</p>
<p>The <a href="qabstractbutton.html#toggled">QAbstractButton::toggled</a>() signal is emitted whenever a checkable button changes its state. The signal's argument is true if the button is checked, or false if the button is unchecked. The <a href="qwidget.html#visible-prop">QWidget::setVisible</a>() slot sets the widget's visible status. If the status is true the widget is shown, otherwise the widget is hidden.</p>
<p>Since we made the <b>More</b> button checkable when we created it, the connection makes sure that the extension widget is shown depending on the state of <b>More</b> button.</p>
<p>We also connect the <b>Close</b> button to the <a href="qwidget.html#close">QWidget::close</a>() slot, and we put the checkboxes associated with the advanced search options into a layout we install on the extension widget.</p>
<pre>     QHBoxLayout *topLeftLayout = new QHBoxLayout;
     topLeftLayout-&gt;addWidget(label);
     topLeftLayout-&gt;addWidget(lineEdit);

     QVBoxLayout *leftLayout = new QVBoxLayout;
     leftLayout-&gt;addLayout(topLeftLayout);
     leftLayout-&gt;addWidget(caseCheckBox);
     leftLayout-&gt;addWidget(fromStartCheckBox);
     leftLayout-&gt;addStretch(1);

     QGridLayout *mainLayout = new QGridLayout;
     mainLayout-&gt;setSizeConstraint(QLayout::SetFixedSize);
     mainLayout-&gt;addLayout(leftLayout, 0, 0);
     mainLayout-&gt;addWidget(buttonBox, 0, 1);
     mainLayout-&gt;addWidget(extension, 1, 0, 1, 2);
     setLayout(mainLayout);

     setWindowTitle(tr(&quot;Extension&quot;));</pre>
<p>Before we create the main layout, we create several child layouts for the widgets: First we allign the <a href="qlabel.html">QLabel</a> ans its buddy, the <a href="qlineedit.html">QLineEdit</a>, using a <a href="qhboxlayout.html">QHBoxLayout</a>. Then we vertically allign the <a href="qlabel.html">QLabel</a> and <a href="qlineedit.html">QLineEdit</a> with the checkboxes associated with the simple search, using a <a href="qvboxlayout.html">QVBoxLayout</a>. We also create a <a href="qvboxlayout.html">QVBoxLayout</a> for the buttons. In the end we lay out the two latter layouts and the extension widget using a <a href="qgridlayout.html">QGridLayout</a>.</p>
<pre>     extension-&gt;hide();
 }</pre>
<p>Finally, we hide the extension widget using the <a href="qwidget.html#hide">QWidget::hide</a>() function, making the application only show the simple search options when it starts. When the user wants to access the advanced search options, the dialog only needs to change the visibility of the extension widget. Qt's layout management takes care of the dialog's appearance.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
